package com.ruitu.router_module.share;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Environment;

import com.beanu.arad.Arad;
import com.ruitu.router_module.R;
import com.ruitu.router_module.share.uitool.ShareBoard;
import com.ruitu.router_module.share.uitool.SnsPlatform;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.api.ShareParams;
import cn.jiguang.share.qqmodel.QQ;
import cn.jiguang.share.qqmodel.QZone;
import cn.jiguang.share.wechat.Wechat;
import cn.jiguang.share.wechat.WechatMoments;

/**
 * Created by Administrator on 2018/4/28.
 */
public class ShareUtil {

    private static ShareBoard mShareBoard;

    public static ShareBoard getShareBoard(Activity act) {
        mShareBoard = new ShareBoard(act);
        mShareBoard.addPlatform(createSnsPlatform(Wechat.Name));
        mShareBoard.addPlatform(createSnsPlatform(WechatMoments.Name));
        mShareBoard.addPlatform(createSnsPlatform(QQ.Name));
        mShareBoard.addPlatform(createSnsPlatform(QZone.Name));

//            List<String> platforms = JShareInterface.getPlatformList();
//            if (platforms != null) {
//                Iterator var2 = platforms.iterator();
//                while (var2.hasNext()) {
//                    String temp = (String) var2.next();
//                    SnsPlatform snsPlatform = createSnsPlatform(temp);
//                    mShareBoard.addPlatform(snsPlatform);
//                }
//            }
        return mShareBoard;
    }

    public static SnsPlatform createSnsPlatform(String platformName) {
        String mShowWord = platformName;
        String mIcon = "";
        String mGrayIcon = "";
        String mKeyword = platformName;
        if (Wechat.Name.equals(platformName)) {
            mIcon = "jiguang_socialize_wechat";
            mGrayIcon = "jiguang_socialize_wechat";
            mShowWord = "jiguang_socialize_text_weixin_key";
        } else if (WechatMoments.Name.equals(platformName)) {
            mIcon = "jiguang_socialize_wxcircle";
            mGrayIcon = "jiguang_socialize_wxcircle";
            mShowWord = "jiguang_socialize_text_weixin_circle_key";
        } else if (QQ.Name.equals(platformName)) {
            mIcon = "jiguang_socialize_qq";
            mGrayIcon = "jiguang_socialize_qq";
            mShowWord = "jiguang_socialize_text_qq_key";
        } else if (QZone.Name.equals(platformName)) {
            mIcon = "jiguang_socialize_qzone";
            mGrayIcon = "jiguang_socialize_qzone";
            mShowWord = "jiguang_socialize_text_qq_zone_key";
        }

//        else if (WechatFavorite.Name.equals(platformName)) {
//            mIcon = "jiguang_socialize_wxfavorite";
//            mGrayIcon = "jiguang_socialize_wxfavorite";
//            mShowWord = "jiguang_socialize_text_weixin_favorite_key";
//        }

//        else if (SinaWeibo.Name.equals(platformName)) {
//            mIcon = "jiguang_socialize_sina";
//            mGrayIcon = "jiguang_socialize_sina";
//            mShowWord = "jiguang_socialize_text_sina_key";
//        }
//        else if (SinaWeiboMessage.Name.equals(platformName)) {
//            mIcon = "jiguang_socialize_sina";
//            mGrayIcon = "jiguang_socialize_sina";
//            mShowWord = "jiguang_socialize_text_sina_msg_key";
//        }
//        else if (Facebook.Name.equals(platformName)) {
//            mIcon = "jiguang_socialize_facebook";
//            mGrayIcon = "jiguang_socialize_facebook";
//            mShowWord = "jiguang_socialize_text_facebook_key";
//        }
//        else if (FbMessenger.Name.equals(platformName)) {
//            mIcon = "jiguang_socialize_messenger";
//            mGrayIcon = "jiguang_socialize_messenger";
//            mShowWord = "jiguang_socialize_text_messenger_key";
//        }
//        else if (Twitter.Name.equals(platformName)) {
//            mIcon = "jiguang_socialize_twitter";
//            mGrayIcon = "jiguang_socialize_twitter";
//            mShowWord = "jiguang_socialize_text_twitter_key";
//        }
        return ShareBoard.createSnsPlatform(mShowWord, mKeyword, mIcon, mGrayIcon, 0);
    }

    private static ShareParams shareParams;

    public static ShareParams getShareParams(String title, String txt, String url) {
        if (null == shareParams) {
            shareParams = new ShareParams();
        }
        shareParams.setShareType(Platform.SHARE_WEBPAGE);
        shareParams.setTitle(title);
        shareParams.setText(txt);
        shareParams.setUrl(url);

//        if (null == imgFile) {
//            imgFile = copyResurces("share_icon.png", "share_icon.png", 0);
//        }
//        shareParams.setImagePath(imgFile.getAbsolutePath());

        shareParams.setImageData(BitmapFactory.decodeResource(Arad.app.getResources(), R.drawable.img_default));

        return shareParams;
    }

    private static File imgFile;

    private static File copyResurces(String src, String dest, int flag) {
        File filesDir = null;
        try {
            if (flag == 0) {//copy to sdcard
                filesDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/jiuduhui/" + dest);
                File parentDir = filesDir.getParentFile();
                if (!parentDir.exists()) {
                    parentDir.mkdirs();
                }
            } else {//copy to data
                filesDir = new File(Arad.app.getFilesDir(), dest);
            }
            if (!filesDir.exists()) {
                filesDir.createNewFile();
                InputStream open = Arad.app.getAssets().open(src);
                FileOutputStream fileOutputStream = new FileOutputStream(filesDir);
                byte[] buffer = new byte[4 * 1024];
                int len = 0;
                while ((len = open.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, len);
                }
                open.close();
                fileOutputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            if (flag == 0) {
                filesDir = copyResurces(src, dest, 1);
            }
        }
        return filesDir;
    }
}
